/*
 *  JVending - JSP Tag Library
 *  Copyright (C) 2004  Shane Isbell
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.jvending.provisioning.taglib;

import java.io.IOException;

import java.util.Iterator;
import java.util.List;

import javax.provisioning.ProvisioningContext;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.BodyContent;

/**
 * Writes the adapters associated with a specific device.
 * @author Shane Isbell
 * @since 1.3.9a
 */

public class AdaptersTag extends BodyTagSupport {

    private Iterator adapterInfos;

    private String deviceId;

    private StringBuffer buffer = new StringBuffer();

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public int doStartTag() throws JspTagException {
        ProvisioningContext provisioningContext = (ProvisioningContext)
             pageContext.getAttribute("javax.provisioning.ProvisioningContext", 
                                       pageContext.APPLICATION_SCOPE);
        if(deviceId == null) deviceId = (String) pageContext.getAttribute("deviceId", pageContext.PAGE_SCOPE);

        if(provisioningContext == null || deviceId == null || deviceId.trim() == "") return SKIP_BODY;
        List adapterInfo = provisioningContext.getAdapterInfos(null, deviceId);
        if(adapterInfo == null || adapterInfo.size() == 0) return SKIP_BODY;
        adapterInfos = adapterInfo.iterator();
        pageContext.setAttribute("adapterInfo", adapterInfos.next());
        return EVAL_BODY_BUFFERED;
    }

    public int doAfterBody() throws JspTagException {     
        BodyContent bodyContent = getBodyContent();
        if(bodyContent != null) {
            buffer.append(bodyContent.getString());
            try {
                bodyContent.clear();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }

       if(adapterInfos.hasNext()) {
            pageContext.setAttribute("adapterInfo", adapterInfos.next());
            return EVAL_BODY_BUFFERED;
        } else
            return SKIP_BODY;
    }

    public int doEndTag() throws JspTagException {
        BodyContent bodyContent = getBodyContent();
        try {
            if(bodyContent != null) bodyContent.getEnclosingWriter().write(buffer.toString());
        } catch(IOException e) {
            e.printStackTrace();
            throw new JspTagException("Could not End Tag");
        } finally {
             buffer = new StringBuffer();
        }
        return EVAL_PAGE;
    }

}
